import {
  core,
  InternalAxiosRequestConfig,
  YmResponse,
  YmResponseCode
} from './core'

function parseMsg(data: unknown) {
  let result: unknown
  if (data && typeof data === 'object') {
    if ('msg' in data) {
      result = data['msg']
    }
    if ('message' in data) {
      result = data['message']
    }
  }
  if (typeof result === 'undefined' || typeof result === 'string') {
    return result
  }
}
//接口形式1以 response.data.suc 为准，接口形式2以response.data.code为准
function parseCode(data: unknown) {
  if (data && typeof data === 'object') {
    if ('suc' in data) {
      if (data['suc'] === true) {
        return YmResponseCode.normal
      }
    }
    if ('code' in data) {
      if (data['code'] === 200) {
        return YmResponseCode.normal
      }
      if (data['code'] === 401) {
        // 没有权限了
        return YmResponseCode.noRoot
      }
      if ('errCode' in data) {
        //获取企业列表接口： /account/ent/user/api/account/coopEnt.htm
        if (data['errCode'] === '5002') {
          // 没有权限了
          return YmResponseCode.noLogin
        }
      }
    }
  }
  return YmResponseCode.unknown
}

function parseData(data: unknown) {
  let total: number | undefined
  let _data: unknown
  if (data && typeof data === 'object') {
    if ('total' in data) {
      if (typeof data['total'] === 'number') {
        total = data['total']
      }
    }

    if ('data' in data) {
      _data = data['data']
    }
    if ('rows' in data) {
      _data = data['rows']
    }
  }

  if (total === 0 || (total && Array.isArray(_data))) {
    return {
      total,
      list: _data ?? []
    }
  } else {
    return _data
  }
}

function isNestData(data: unknown) {
  // 满足嵌套规则

  if (data && typeof data === 'object') {
    if (
      'code' in data &&
      'msg' in data &&
      ('data' in data || 'rows' in data) &&
      'msg' in data
    ) {
      return true
    }
  }
}
interface ConfExpand extends InternalAxiosRequestConfig {
  /**
   * @desc 是否不自动使用{message展示异常}
   * @default {false}
   */
  noAutoShowErrorMessage?: boolean

  /**
   * @desc 是否是 response.data.suc 的返形式
   * @default {false}
   */
  responseAsSuc?: boolean
}

/**
 * @example
 * { post application/x-www-form-urlencoded
 *   request({url:'xxxx',params:{pageIndex:1,pageSize:10}})
 * }
 * { post application/json
 *   request({url:'xxxx',data:{pageIndex:1,pageSize:10}})
 * }
 * { post multipart/form-data // data类型为FormData类型即可
 *   request({url:'xxxx',data:new FormData()})
 * }
 * { get 默认提交方式修改为post,所以如果需要get请求需要自己设置
 *   request({url:'xxxx',params:{pageIndex:1,pageSize:10},method:'get'})
 * }
 */

const transformResponse = (function () {
  function _transformResponse(data: unknown): YmResponse<unknown> {
    const code = parseCode(data)
    const msg = parseMsg(data)
    const _data = parseData(data)

    if (isNestData(_data)) {
      return _transformResponse(_data)
    } else {
      return {
        code:
          _data && code === YmResponseCode.unknown
            ? YmResponseCode.normal
            : code,
        data: _data,
        msg
      }
    }
  }
  return _transformResponse
})()

export const request = core<ConfExpand>({
  transformResponse
})
